package ace.cmp.asserts.core.impl;

import ace.cmp.asserts.api.AssertService;
import ace.cmp.core.exception.BusinessException;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author Caspar
 * @create 2021/2/24 断言检查，抛出{@link BusinessException} 异常
 */
public class DefaultAssertService implements AssertService {
  @Override
  public void isTrue(boolean value, String messageTemplate, String... args) {
    String message = String.format(messageTemplate, args);
    isTrue(value, message);
  }

  @Override
  public void isTrue(boolean value, String message) {
    isTrue(value, () -> new BusinessException(message));
  }

  @Override
  public void isTrue(boolean value, Supplier<RuntimeException> supplier) {
    if (value) {
      throw supplier.get();
    }
  }

  @Override
  public void isFalse(boolean value, String messageTemplate, String... args) {
    isTrue(!value, messageTemplate, args);
  }

  @Override
  public void isFalse(boolean value, String message) {
    isTrue(!value, message);
  }

  @Override
  public void isFalse(boolean value, Supplier<RuntimeException> supplier) {
    isTrue(!value, supplier);
  }

  @Override
  public void isNull(Object value, Supplier<RuntimeException> supplier) {
    isTrue(Objects.isNull(value), supplier);
  }

  @Override
  public void isNull(Object value, String messageTemplate, String... args) {
    isTrue(Objects.isNull(value), messageTemplate, args);
  }

  @Override
  public void isNull(Object value, String message) {
    isTrue(Objects.isNull(value), message);
  }

  @Override
  public void isNotNull(Object value, Supplier<RuntimeException> supplier) {
    isTrue(Objects.nonNull(value), supplier);
  }

  @Override
  public void isNotNull(Object value, String messageTemplate, String... args) {
    isTrue(Objects.nonNull(value), messageTemplate, args);
  }

  @Override
  public void isNotNull(Object value, String message) {
    isTrue(Objects.nonNull(value), message);
  }

  @Override
  public void isEmpty(String value, Supplier<RuntimeException> supplier) {
    isTrue(value == null || value.length() == 0, supplier);
  }

  @Override
  public void isEmpty(String value, String message) {
    isTrue(value == null || value.length() == 0, message);
  }

  @Override
  public void isEmpty(String value, String messageTemplate, String... args) {
    isTrue(value == null || value.length() == 0, messageTemplate, args);
  }

  @Override
  public void isEmpty(Collection value, Supplier<RuntimeException> supplier) {
    isTrue(value == null || value.size() == 0, supplier);
  }

  @Override
  public void isEmpty(Collection value, String message) {
    isTrue(value == null || value.size() == 0, message);
  }

  @Override
  public void isEmpty(Collection value, String messageTemplate, String... args) {
    isTrue(value == null || value.size() == 0, messageTemplate, args);
  }

  @Override
  public void isEmpty(Map value, Supplier<RuntimeException> supplier) {
    isTrue(value == null || value.size() == 0, supplier);
  }

  @Override
  public void isEmpty(Map value, String message) {
    isTrue(value == null || value.size() == 0, message);
  }

  @Override
  public void isEmpty(Map value, String messageTemplate, String... args) {
    isTrue(value == null || value.size() == 0, messageTemplate, args);
  }
}
